"use strict";
//index.js
//获取应用实例
var app = getApp()
var util = require('../../../utils/util.js')
var common = require('../../../utils/api.js')
var toast = require('../../../utils/toast.js')
var api = common.api
Page({
  data: {
    initdate:'',
    motto: 'Hello World',
    headTitle: '创建项目',
    userInfo: {},
    manageslist:[],
    teamleaderslist:[],
    scrollViewHeight: 200,
    selectmanage:'',
    selectteamleader:'',
    productstatus:util.productstatus,
    pid:null,
    wetoast:{
      reveal:false,
      title:'',
      animationData:{}
    },
    statusselect:'',
    status:''
  },
  //事件处理函数
  bindViewTap: function() {
  },
  onShow:function(){
console.log('onShow')
  },
  onLoad:function(options){
    util.showLoading({title: '加载中'})
    this.toast = toast
    //创建可重复使用的WeToast实例，并附加到this上，通过this.wetoast访问
    var that = this
    console.log('onLoad',options)
    var d = new Date()
    var startdate = util.formatDate(d)
    console.log('开始时间',startdate,app.manageslist)
    this.data.pid = options.pid
    // 页面初始化 options为页面跳转所带来的参数
    
    api.getProductInfo(options.pid,function(result){
      var teamleaders = util.getSelectteamleaderslist(result.teamleader)
      console.log('getProductInfo',result,that.data.productstatus,that.data.headTitle,teamleaders,util.getstatus(result.status))
      that.setData({product:result})
      that.setData({
      selectmanage:util.getSelectmanage(result.manager),
      selectteamleader:result.teamleader,
       manageslist:app.manageslist,          teamleaderslist:teamleaders,
    startdate:startdate,
    statusselect:util.getstatus(result.status),
    status:result.status,
    initdate:result.productdate})
      util.hideLoading()
      
    })
  },
  scroll: function(e) {
    console.log(e)
  },
  bindDateChange:function(e){
     this.setData({
      initdate: e.detail.value
    })
  },
  bindManageslistChange:function(e){
    var index = e.detail.value
    var name = this.data.manageslist[index].name
    console.log("选择项目经理",name,index)
    this.setData({
      selectmanage: name
    })
  },
  bindStatuslistChange:function(e){
    var index = e.detail.value
    var name = this.data.productstatus[index].name
    var status = this.data.productstatus[index].status
    console.log("状态",name,status)
    this.setData({
      statusselect: name,
      status:status
    })
  },
    checkboxChange:function(e){
    var index = e.detail.value
    console.log("选择技术经理",index)
    this.setData({
      selectteamleader: index.join()
    })
  },
  bindFormSubmit: function(e) {
    var that = this
    var formdata = e.detail.value
    var toasttitle = ""
    if(!formdata.productdescript){
      toasttitle="项目描述未填写"
    }
    if(!formdata.title){
      toasttitle="项目标题未填写"
    }
    if(toasttitle){
      console.log(this)
      this.toast(toasttitle)
    }else{
      var manageid = util.getSelectmanageid(this.data.selectmanage)
      var data = {
        title:formdata.title,
        productdescript:formdata.productdescript,
        manager:manageid,
        teamleader:this.data.selectteamleader,
        productdate:formdata.productdate,
        owner:app.globalData.userInfo.id,
        status:this.data.status
      }
      console.log(128,data);
     //调用创建项目的接口
      api.setProductInfo(this.data.pid,data,function(result){
        console.log(data,result)
        //更新数据
        if(result.errno==0){
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000,
            complete:function(){
              wx.switchTab({
                url: '/pages/index/index/index'
              })
            }
          })
        }else{
          that.toast(result.errmsg)
        }
      })
    }
  }
})
